// pages/Goods/teamStyle/teamDetail/index.js
const common = require("../../../../../public.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    token: '',
    openid: '',
    likeFlag: false,
    store: {},
    storeTeam: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid,
          teamId: options.teamId
        }, _ => {
          that.getStoreTeamDetail()
        })

      },
      fail: function (res) {
        common.showLoginPage()
      }
    })
  },

  /**
   * 获取团队详情
   */
  getStoreTeamDetail() {
    const that = this
    const tempJson = {
      userId: this.data.userInfo.id,
      teamId: this.data.teamId
    }
    common.post('/store/getStoreTeamDetail', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          likeFlag: res.data.likeFlag,
          store: res.data.store,
          storeTeam: res.data.storeTeam,
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.storeTeam.phone
    })
  },
  checkAddress() { //点击地址按钮
    let lon = parseFloat(this.data.store.xpoint)
    let lat = parseFloat(this.data.store.ypoint)
    wx.openLocation({
      latitude: lat,
      longitude: lon,
      scale: 18,
      fail: () => {
        common.showToast('无地址信息')
      }
    })
  },
  /**
   * 点赞
   */
  clickAwesome() {
    if (this.data.likeFlag == true) { //说名已经是点过赞  走取消点赞逻辑
      this.praiseAction(false)
    } else {
      this.praiseAction(true)
    }
  },
  /**
   * 提交点赞
   * @param {boolean} save 保存或删除点赞信息
   */
  praiseAction(save) {
    const that = this
    const param = {
      userId: that.data.userInfo.id,
      teamId: that.data.teamId,
      token: that.data.token
    }
    const url = save ? '/store/saveStoreTeamPraise' : '/store/delStoreTeamPraise'
    common.post(url, param, function (res) {
      if (res.status === 200) {
        that.getStoreTeamDetail()
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  watchQRcode() {
    wx.previewImage({
      current: this.data.storeTeam.wxQrcode, // 当前显示图片的http链接
      urls: [this.data.storeTeam.wxQrcode] // 需要预览的图片http链接列表
    })
  }
})